package com.fire.eyes.controller;

import com.alibaba.fastjson.JSON;
import com.fire.core.base.controller.BaseController;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.BlogUtil;
import com.fire.core.util.ToolUtil;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.service.IRecommendRuleService;
import com.fire.eyes.service.ITMitActivityService;
import com.fire.eyes.service.ITMitMaketContentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Map;


/**
 * 商品接口*
 * @author pll
 *@date： 日期：2018年4月24日 时间：下午4:12:19
 *
 */

@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {
	private final static Logger blogin = Logger.getLogger(GoodsController.class);
	@Autowired
	private ITMitActivityService activityService;
	@Resource
	private ITMitErrorCodeService itMitErrorCodeService;
	@Autowired
	private ITMitMaketContentService iTMitMaketContentService;
	@Autowired
	private IRecommendRuleService recommendRuleService;

	/**
	 * 获取活动activity列表接口
	 * @author raoyuxing
	 * @param json 传入参数 chanelid
	 * @return
	 */
	@RequestMapping(value = "/getactivity")
	@ResponseBody
	public AjaxResult getactivity(@RequestBody String json) {
		blogin.info("GoodsController.getactivity获取到的json消息" + json);
		BlogUtil.inlog(json,"","/mit/goods/getactivity","getactivity","req");
		Map map = json2Map(json);
		AjaxResult ajaxResult = new AjaxResult();
		
		//效验商品编号是否为空
		String channelid = "chanelid";
		if (ToolUtil.isEmpty(map.get(channelid))) {
			blogin.error("GoodsController.getactivity().error  错误为：渠道编号为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "渠道编号为空", "请输入渠道编号"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","/mit/goods/getactivity","getactivity","resp");
			return ajaxResult;
		} 
		Map resultMap = null;
		try {
			resultMap = activityService.queryActivityList(map);
		}catch (SQLException sqle){
			blogin.error("GoodsController.getactivity().error  错误为：",sqle);
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SYS_DATAERROR,"数据库异常","系统错误"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","/mit/goods/getactivity","getactivity","resp");
			return ajaxResult;
		} catch (Exception e) {
			blogin.error("GoodsController.getactivity().error  错误为：",e);
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SYS_DATAERROR,"系统错误","系统错误"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","/mit/goods/getactivity","getactivity","resp");
			return ajaxResult;
		}
		ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",resultMap, "操作成功"));
		BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","/mit/goods/getactivity","getactivity","resp");
		return ajaxResult;
	}

	/**
	 * 查询营销内容
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/marketcontent")
	@ResponseBody
	public AjaxResult queryMarketcontent(@RequestBody String json){
		blogin.info("========查询营销内容开始========");
		blogin.info("查询营销内容:"+json);
		AjaxResult ajaxResult = new AjaxResult();
		try {
			ajaxResult = iTMitMaketContentService.queryMarketcontent(json);
		} catch (Exception e) {
			blogin.error("查询营销内容接口错误===>" + e.getMessage());
			e.printStackTrace();
			ajaxResult.setStatus(ErrorCodeConstants.INTERNAL_ERROR);
			ajaxResult.setMessage("系统异常");
			ajaxResult.setTips("系统异常");
		}
		
		String tips = itMitErrorCodeService.getTips(ajaxResult.getStatus()); 
    	if(StringUtils.isNotBlank(tips)) {
    		ajaxResult.setTips("["+ajaxResult.getStatus()+"]"+tips);
    	}else {
    		ajaxResult.setTips("["+ajaxResult.getStatus()+"]"+ajaxResult.getTips());
    	}
		blogin.info("查询营销内容响应消息:"+ajaxResult.toString());
		blogin.info("========查询营销内容结束========");
		return ajaxResult;
	}
	
	/**
	 * 查询推荐内容
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/getrecomend")
	@ResponseBody
	public AjaxResult getrecomend(@RequestBody String json){
		blogin.info("========查询推荐内容开始========");
		blogin.info("查询推荐内容:"+json);
		AjaxResult ajaxResult = new AjaxResult();
		try {
			ajaxResult = recommendRuleService.getrecomend(json);
		} catch (Exception e) {
			blogin.error("查询推荐内容接口错误===>" + e.getMessage());
			e.printStackTrace();
			ajaxResult.setStatus(ErrorCodeConstants.INTERNAL_ERROR);
			ajaxResult.setMessage("系统异常");
			ajaxResult.setTips("系统异常");
		}
		
		String tips = itMitErrorCodeService.getTips(ajaxResult.getStatus()); 
    	if(StringUtils.isNotBlank(tips)) {
    		ajaxResult.setTips("["+ajaxResult.getStatus()+"]"+tips);
    	}else {
    		ajaxResult.setTips("["+ajaxResult.getStatus()+"]"+ajaxResult.getTips());
    	}
		blogin.info("查询推荐内容响应消息:"+ajaxResult.toString());
		blogin.info("========查询推荐内容结束========");
		return ajaxResult;
	}
	
}
